package com.demo.service;

import com.demo.entity.Apple;
import com.demo.entity.Fruits;
import com.demo.entity.Mango;
import com.demo.entity.Strawberry;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;

public class BuyFruitsService {



    public static void main(String[] args) {
            //10斤苹果，3斤草莓
            System.out.println("十斤苹果，三斤草莓,总价等于="+getTotalPriceA());
            //10斤苹果，3斤草莓,2斤芒果
            System.out.println("十斤苹果，三斤草莓,两斤芒果，总价等于="+getTotalPriceB());
            //水果打折 //10斤苹果，3斤草莓,2斤芒果
            System.out.println("十斤苹果，三斤草莓,两斤芒果，草莓八折，总价等于="+getTotalPriceC());
            //买满100减10块
            System.out.println("十斤苹果，三斤草莓,两斤芒果，满100减10块，总价等于="+getTotalPriceD());

    }

    /**
     * 10斤苹果，3斤草莓
     * @return
     */
    public static  BigDecimal getTotalPriceA(){
        Apple apple=new Apple();
        apple.setUninitPrice(new BigDecimal(8));
        apple.setWeight(new BigDecimal(10));
        apple.setDiscount(new BigDecimal(1));
        calculationSingleFruitsPrice(apple);
        Strawberry strawberry=new Strawberry();
        strawberry.setUninitPrice(new BigDecimal(13));
        strawberry.setWeight(new BigDecimal(3));
        strawberry.setDiscount(new BigDecimal(1));
        calculationSingleFruitsPrice(strawberry);
        return apple.getTotalPrice().add(strawberry.getTotalPrice()).setScale(2,BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 10斤苹果，3斤草莓,2斤芒果
     * @return
     */
    public static  BigDecimal getTotalPriceB(){
        Apple apple=new Apple();
        apple.setUninitPrice(new BigDecimal(8));
        apple.setWeight(new BigDecimal(10));
        apple.setDiscount(new BigDecimal(1));
        calculationSingleFruitsPrice(apple);
        Strawberry strawberry=new Strawberry();
        strawberry.setUninitPrice(new BigDecimal(13));
        strawberry.setWeight(new BigDecimal(3));
        strawberry.setDiscount(new BigDecimal(1));
        calculationSingleFruitsPrice(strawberry);
        Mango mango=new Mango();
        mango.setUninitPrice(new BigDecimal(20));
        mango.setWeight(new BigDecimal(2));
        mango.setDiscount(new BigDecimal(1));
        calculationSingleFruitsPrice(mango);
        return apple.getTotalPrice().add(strawberry.getTotalPrice()).add(mango.getTotalPrice())
                .setScale(2,BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 10斤苹果，3斤草莓,2斤芒果,草莓八折
     * @return
     */
    public static  BigDecimal getTotalPriceC(){
        Apple apple=new Apple();
        apple.setUninitPrice(new BigDecimal(8));
        apple.setWeight(new BigDecimal(10));
        apple.setDiscount(new BigDecimal(1));
        calculationSingleFruitsPrice(apple);
        Strawberry strawberry=new Strawberry();
        strawberry.setUninitPrice(new BigDecimal(13));
        strawberry.setWeight(new BigDecimal(3));
        strawberry.setDiscount(new BigDecimal(0.8));
        calculationSingleFruitsPrice(strawberry);
        Mango mango=new Mango();
        mango.setUninitPrice(new BigDecimal(20));
        mango.setWeight(new BigDecimal(2));
        mango.setDiscount(new BigDecimal(1));
        calculationSingleFruitsPrice(mango);
        return apple.getTotalPrice().add(strawberry.getTotalPrice()).add(mango.getTotalPrice()).setScale(2,BigDecimal.ROUND_HALF_UP);
    }



    /**
     * 10斤苹果，3斤草莓,2斤芒果,满100，减10块
     * @return
     */
    public static  BigDecimal getTotalPriceD(){
        Apple apple=new Apple();
        apple.setUninitPrice(new BigDecimal(8));
        apple.setWeight(new BigDecimal(10));
        apple.setDiscount(new BigDecimal(1));
        calculationSingleFruitsPrice(apple);
        Strawberry strawberry=new Strawberry();
        strawberry.setUninitPrice(new BigDecimal(13));
        strawberry.setWeight(new BigDecimal(3));
        strawberry.setDiscount(new BigDecimal(1));
        calculationSingleFruitsPrice(strawberry);
        Mango mango=new Mango();
        mango.setUninitPrice(new BigDecimal(20));
        mango.setWeight(new BigDecimal(2));
        mango.setDiscount(new BigDecimal(1));
        calculationSingleFruitsPrice(mango);
        BigDecimal totalPrice=apple.getTotalPrice().add(strawberry.getTotalPrice()).add(mango.getTotalPrice());
        return totalPrice.subtract(totalPrice.divide(new BigDecimal(100))
                .setScale(0,BigDecimal.ROUND_DOWN).multiply(new BigDecimal(10)))
                .setScale(2,BigDecimal.ROUND_HALF_UP);
    }




    /**
     * 单个水果价格计算
     * @param fruits
     */
    public static void calculationSingleFruitsPrice(Fruits fruits){
        fruits.setTotalPrice(fruits.getUninitPrice()
                                .multiply(fruits.getDiscount())
                                    .multiply(fruits.getWeight()));
    }




}
